home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb / sprite / values.c < prev   
Encoding:
C/C++ Source or Header  |  1990-12-10  |  27.2 KB  |  1,051 lines

  1. /* Low level packing and unpacking of values for GDB.
  2.    Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. GDB is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GDB is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GDB; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include <stdio.h>
  21. #include "defs.h"
  22. #include "param.h"
  23. #include "symtab.h"
  24. #include "value.h"
  25.  
  26. /* The value-history records all the values printed
  27.    by print commands during this session.  Each chunk
  28.    records 60 consecutive values.  The first chunk on
  29.    the chain records the most recent values.
  30.    The total number of values is in value_history_count.  */
  31.  
  32. #define VALUE_HISTORY_CHUNK 60
  33.  
  34. struct value_history_chunk
  35. {
  36.   struct value_history_chunk *next;
  37.   value values[VALUE_HISTORY_CHUNK];
  38. };
  39.  
  40. /* Chain of chunks now in use.  */
  41.  
  42. static struct value_history_chunk *value_history_chain;
  43.  
  44. static int value_history_count;    /* Abs number of last entry stored */
  45.  
  46.  
  47. /* List of all value objects currently allocated
  48.    (except for those released by calls to release_value)
  49.    This is so they can be freed after each command.  */
  50.  
  51. static value all_values;
  52.  
  53. /* Allocate a  value  that has the correct length for type TYPE.  */
  54.  
  55. value
  56. allocate_value (type)
  57.      struct type *type;
  58. {
  59.   register value val;
  60.  
  61.   val = (value) xmalloc (sizeof (struct value) + TYPE_LENGTH (type));
  62.   VALUE_NEXT (val) = all_values;
  63.   all_values = val;
  64.   VALUE_TYPE (val) = type;
  65.   VALUE_LVAL (val) = not_lval;
  66.   VALUE_ADDRESS (val) = 0;
  67.   VALUE_FRAME (val) = 0;
  68.   VALUE_OFFSET (val) = 0;
  69.   VALUE_BITPOS (val) = 0;
  70.   VALUE_BITSIZE (val) = 0;
  71.   VALUE_REPEATED (val) = 0;
  72.   VALUE_REPETITIONS (val) = 0;
  73.   VALUE_REGNO (val) = -1;
  74.   return val;
  75. }
  76.  
  77. /* Allocate a  value  that has the correct length
  78.    for COUNT repetitions type TYPE.  */
  79.  
  80. value
  81. allocate_repeat_value (type, count)
  82.      struct type *type;
  83.      int count;
  84. {
  85.   register value val;
  86.  
  87.   val = (value) xmalloc (sizeof (struct value) + TYPE_LENGTH (type) * count);
  88.   VALUE_NEXT (val) = all_values;
  89.   all_values = val;
  90.   VALUE_TYPE (val) = type;
  91.   VALUE_LVAL (val) = not_lval;
  92.   VALUE_ADDRESS (val) = 0;
  93.   VALUE_FRAME (val) = 0;
  94.   VALUE_OFFSET (val) = 0;
  95.   VALUE_BITPOS (val) = 0;
  96.   VALUE_BITSIZE (val) = 0;
  97.   VALUE_REPEATED (val) = 1;
  98.   VALUE_REPETITIONS (val) = count;
  99.   VALUE_REGNO (val) = -1;
  100.   return val;
  101. }
  102.  
  103. /* Free all the values that have been allocated (except for those released).
  104.    Called after each command, successful or not.  */
  105.  
  106. void
  107. free_all_values ()
  108. {
  109.   register value val, next;
  110.  
  111.   for (val = all_values; val; val = next)
  112.     {
  113.       next = VALUE_NEXT (val);
  114.       free (val);
  115.     }
  116.  
  117.   all_values = 0;
  118. }
  119.  
  120. /* Remove VAL from the chain all_values
  121.    so it will not be freed automatically.  */
  122.  
  123. void
  124. release_value (val)
  125.      register value val;
  126. {
  127.   register value v;
  128.  
  129.   if (all_values == val)
  130.     {
  131.       all_values = val->next;
  132.       return;
  133.     }
  134.  
  135.   for (v = all_values; v; v = v->next)
  136.     {
  137.       if (v->next == val)
  138.     {
  139.       v->next = val->next;
  140.       break;
  141.     }
  142.     }
  143. }
  144.  
  145. /* Return a copy of the value ARG.
  146.    It contains the same contents, for same memory address,
  147.    but it's a different block of storage.  */
  148.  
  149. static value
  150. value_copy (arg)
  151.      value arg;
  152. {
  153.   register value val;
  154.   register struct type *type = VALUE_TYPE (arg);
  155.   if (VALUE_REPEATED (arg))
  156.     val = allocate_repeat_value (type, VALUE_REPETITIONS (arg));
  157.   else
  158.     val = allocate_value (type);
  159.   VALUE_LVAL (val) = VALUE_LVAL (arg);
  160.   VALUE_ADDRESS (val) = VALUE_ADDRESS (arg);
  161.   VALUE_OFFSET (val) = VALUE_OFFSET (arg);
  162.   VALUE_BITPOS (val) = VALUE_BITPOS (arg);
  163.   VALUE_BITSIZE (val) = VALUE_BITSIZE (arg);
  164.   VALUE_REGNO (val) = VALUE_REGNO (arg);
  165.   bcopy (VALUE_CONTENTS (arg), VALUE_CONTENTS (val),
  166.      TYPE_LENGTH (VALUE_TYPE (arg))
  167.      * (VALUE_REPEATED (arg) ? VALUE_REPETITIONS (arg) : 1));
  168.   return val;
  169. }
  170.  
  171. /* Access to the value history.  */
  172.  
  173. /* Record a new value in the value history.
  174.    Returns the absolute history index of the entry.  */
  175.  
  176. int
  177. record_latest_value (val)
  178.      value val;
  179. {
  180.   int i;
  181.   double foo;
  182.  
  183.   /* Check error now if about to store an invalid float.  We return -1
  184.      to the caller, but allow them to continue, e.g. to print it as "Nan". */
  185.   if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FLT) {
  186.     foo = unpack_double (VALUE_TYPE (val), VALUE_CONTENTS (val), &i);
  187.     if (i) return -1;        /* Indicate value not saved in history */
  188.   }
  189.  
  190.   /* Here we treat value_history_count as origin-zero
  191.      and applying to the value being stored now.  */
  192.  
  193.   i = value_history_count % VALUE_HISTORY_CHUNK;
  194.   if (i == 0)
  195.     {
  196.       register struct value_history_chunk *new
  197.     = (struct value_history_chunk *)
  198.       xmalloc (sizeof (struct value_history_chunk));
  199.       bzero (new->values, sizeof new->values);
  200.       new->next = value_history_chain;
  201.       value_history_chain = new;
  202.     }
  203.  
  204.   value_history_chain->values[i] = val;
  205.   release_value (val);
  206.  
  207.   /* Now we regard value_history_count as origin-one
  208.      and applying to the value just stored.  */
  209.  
  210.   return ++value_history_count;
  211. }
  212.  
  213. /* Return a copy of the value in the history with sequence number NUM.  */
  214.  
  215. value
  216. access_value_history (num)
  217.      int num;
  218. {
  219.   register struct value_history_chunk *chunk;
  220.   register int i;
  221.   register int absnum = num;
  222.  
  223.   if (absnum <= 0)
  224.     absnum += value_history_count;
  225.  
  226.   if (absnum <= 0)
  227.     {
  228.       if (num == 0)
  229.     error ("The history is empty.");
  230.       else if (num == 1)
  231.     error ("There is only one value in the history.");
  232.       else
  233.     error ("History does not go back to $$%d.", -num);
  234.     }
  235.   if (absnum > value_history_count)
  236.     error ("History has not yet reached $%d.", absnum);
  237.  
  238.   absnum--;
  239.  
  240.   /* Now absnum is always absolute and origin zero.  */
  241.  
  242.   chunk = value_history_chain;
  243.   for (i = (value_history_count - 1) / VALUE_HISTORY_CHUNK - absnum / VALUE_HISTORY_CHUNK;
  244.        i > 0; i--)
  245.     chunk = chunk->next;
  246.  
  247.   return value_copy (chunk->values[absnum % VALUE_HISTORY_CHUNK]);
  248. }
  249.  
  250. /* Clear the value history entirely.
  251.    Must be done when new symbol tables are loaded,
  252.    because the type pointers become invalid.  */
  253.  
  254. void
  255. clear_value_history ()
  256. {
  257.   register struct value_history_chunk *next;
  258.   register int i;
  259.   register value val;
  260.  
  261.   while (value_history_chain)
  262.     {
  263.       for (i = 0; i < VALUE_HISTORY_CHUNK; i++)
  264.     if (val = value_history_chain->values[i])
  265.       free (val);
  266.       next = value_history_chain->next;
  267.       free (value_history_chain);
  268.       value_history_chain = next;
  269.     }
  270.   value_history_count = 0;
  271. }
  272.  
  273. static void
  274. value_history_info (num_exp, from_tty)
  275.      char *num_exp;
  276.      int from_tty;
  277. {
  278.   register int i;
  279.   register value val;
  280.   static int num = 1;
  281.  
  282.   if (num_exp)
  283.     {
  284.       if (num_exp[0] == '+' && num_exp[1] == '\0')
  285.     /* "info history +" should print from the stored position.  */
  286.     ;
  287.       else
  288.     /* "info history <exp>" should print around value number <exp>.  */
  289.     num = parse_and_eval_address (num_exp) - 5;
  290.     }
  291.   else
  292.     {
  293.       /* "info history" means print the last 10 values.  */
  294.       num = value_history_count - 9;
  295.     }
  296.  
  297.   if (num <= 0)
  298.     num = 1;
  299.  
  300.   for (i = num; i < num + 10 && i <= value_history_count; i++)
  301.     {
  302.       val = access_value_history (i);
  303.       printf_filtered ("$%d = ", i);
  304.       value_print (val, stdout, 0, Val_pretty_default);
  305.       printf_filtered ("\n");
  306.     }
  307.  
  308.   /* The next "info history +" should start after what we just printed.  */
  309.   num += 10;
  310.  
  311.   /* Hitting just return after this command should do the same thing as
  312.      "info history +".  If num_exp is null, this is unnecessary, since
  313.      "info history +" is not useful after "info history".  */
  314.   if (from_tty && num_exp)
  315.     {
  316.       num_exp[0] = '+';
  317.       num_exp[1] = '\0';
  318.     }
  319. }
  320.  
  321. /* Internal variables.  These are variables within the debugger
  322.    that hold values assigned by debugger commands.
  323.    The user refers to them with a '$' prefix
  324.    that does not appear in the variable names stored internally.  */
  325.  
  326. static struct internalvar *internalvars;
  327.  
  328. /* Look up an internal variable with name NAME.  NAME should not
  329.    normally include a dollar sign.
  330.  
  331.    If the specified internal variable does not exist,
  332.    one is created, with a void value.  */
  333.  
  334. struct internalvar *
  335. lookup_internalvar (name)
  336.      char *name;
  337. {
  338.   register struct internalvar *var;
  339.  
  340.   for (var = internalvars; var; var = var->next)
  341.     if (!strcmp (var->name, name))
  342.       return var;
  343.  
  344.   var = (struct internalvar *) xmalloc (sizeof (struct internalvar));
  345.   var->name = concat (name, "", "");
  346.   var->value = allocate_value (builtin_type_void);
  347.   release_value (var->value);
  348.   var->next = internalvars;
  349.   internalvars = var;
  350.   return var;
  351. }
  352.  
  353. value
  354. value_of_internalvar (var)
  355.      struct internalvar *var;
  356. {
  357.   register value val;
  358.  
  359. #ifdef IS_TRAPPED_INTERNALVAR
  360.   if (IS_TRAPPED_INTERNALVAR (var->name))
  361.     return VALUE_OF_TRAPPED_INTERNALVAR (var);
  362. #endif 
  363.  
  364.   val = value_copy (var->value);
  365.   VALUE_LVAL (val) = lval_internalvar;
  366.   VALUE_INTERNALVAR (val) = var;
  367.   return val;
  368. }
  369.  
  370. void
  371. set_internalvar_component (var, offset, bitpos, bitsize, newval)
  372.      struct internalvar *var;
  373.      int offset, bitpos, bitsize;
  374.      value newval;
  375. {
  376.   register char *addr = VALUE_CONTENTS (var->value) + offset;
  377.  
  378. #ifdef IS_TRAPPED_INTERNALVAR
  379.   if (IS_TRAPPED_INTERNALVAR (var->name))
  380.     SET_TRAPPED_INTERNALVAR (var, newval, bitpos, bitsize, offset);
  381. #endif
  382.  
  383.   if (bitsize)
  384.     modify_field (addr, (int) value_as_long (newval),
  385.           bitpos, bitsize);
  386.   else
  387.     bcopy (VALUE_CONTENTS (newval), addr,
  388.        TYPE_LENGTH (VALUE_TYPE (newval)));
  389. }
  390.  
  391. void
  392. set_internalvar (var, val)
  393.      struct internalvar *var;
  394.      value val;
  395. {
  396. #ifdef IS_TRAPPED_INTERNALVAR
  397.   if (IS_TRAPPED_INTERNALVAR (var->name))
  398.     SET_TRAPPED_INTERNALVAR (var, val, 0, 0, 0);
  399. #endif
  400.  
  401.   free (var->value);
  402.   var->value = value_copy (val);
  403.   release_value (var->value);
  404. }
  405.  
  406. char *
  407. internalvar_name (var)
  408.      struct internalvar *var;
  409. {
  410.   return var->name;
  411. }
  412.  
  413. /* Free all internalvars.  Done when new symtabs are loaded,
  414.    because that makes the values invalid.  */
  415.  
  416. void
  417. clear_internalvars ()
  418. {
  419.   register struct internalvar *var;
  420.  
  421.   while (internalvars)
  422.     {
  423.       var = internalvars;
  424.       internalvars = var->next;
  425.       free (var->name);
  426.       free (var->value);
  427.       free (var);
  428.     }
  429. }
  430.  
  431. static void
  432. convenience_info ()
  433. {
  434.   register struct internalvar *var;
  435.   int varseen = 0;
  436.  
  437.   for (var = internalvars; var; var = var->next)
  438.     {
  439. #ifdef IS_TRAPPED_INTERNALVAR
  440.       if (IS_TRAPPED_INTERNALVAR (var->name))
  441.     continue;
  442. #endif
  443.       if (!varseen)
  444.     {
  445.       printf ("Debugger convenience variables:\n\n");
  446.       varseen = 1;
  447.     }
  448.       printf ("$%s: ", var->name);
  449.       value_print (var->value, stdout, 0, Val_pretty_default);
  450.       printf ("\n");
  451.     }
  452.   if (!varseen)
  453.     printf ("No debugger convenience variables now defined.\n\
  454. Convenience variables have names starting with \"$\";\n\
  455. use \"set\" as in \"set $foo = 5\" to define them.\n");
  456. }
  457.  
  458. /* Extract a value as a C number (either long or double).
  459.    Knows how to convert fixed values to double, or
  460.    floating values to long.
  461.    Does not deallocate the value.  */
  462.  
  463. LONGEST
  464. value_as_long (val)
  465.      register value val;
  466. {
  467.   return unpack_long (VALUE_TYPE (val), VALUE_CONTENTS (val));
  468. }
  469.  
  470. double
  471. value_as_double (val)
  472.      register value val;
  473. {
  474.   double foo;
  475.   int inv;
  476.   
  477.   foo = unpack_double (VALUE_TYPE (val), VALUE_CONTENTS (val), &inv);
  478.   if (inv)
  479.     error ("Invalid floating value found in program.");
  480.   return foo;
  481. }
  482.  
  483. /* Unpack raw data (copied from debugee) at VALADDR
  484.    as a long, or as a double, assuming the raw data is described
  485.    by type TYPE.  Knows how to convert different sizes of values
  486.    and can convert between fixed and floating point.
  487.  
  488.    C++: It is assumed that the front-end has taken care of
  489.    all matters concerning pointers to members.  A pointer
  490.    to member which reaches here is considered to be equivalent
  491.    to an INT (or some size).  After all, it is only an offset.  */
  492.  
  493. /*
  494.  * To allow cross machine type debugging in Sprite we bcopy the 
  495.  * data before returning it.  This is done so we can debugger sun3
  496.  * from sun4 that have stricter alignment constraints.
  497.  */
  498.  
  499. LONGEST
  500. unpack_long (type, valaddr)
  501.      struct type *type;
  502.      char *valaddr;
  503. {
  504.   register enum type_code code = TYPE_CODE (type);
  505.   register int len = TYPE_LENGTH (type);
  506.   register int nosign = TYPE_UNSIGNED (type);
  507. #ifdef KGDB
  508.   union { 
  509.    double    foo; /* double forces alignment. */
  510.    char    buffer[8];
  511.   } tmpMem;
  512.   bcopy(valaddr,tmpMem.buffer,8);
  513.   valaddr = tmpMem.buffer;
  514. #endif
  515.  
  516.   if (code == TYPE_CODE_ENUM)
  517.     code = TYPE_CODE_INT;
  518.   if (code == TYPE_CODE_FLT)
  519.     {
  520.       if (len == sizeof (float))
  521.     return * (float *) valaddr;
  522.  
  523.       if (len == sizeof (double))
  524.     return * (double *) valaddr;
  525.     }
  526.   else if (code == TYPE_CODE_INT && nosign)
  527.     {
  528.       if (len == sizeof (char))
  529.     return * (unsigned char *) valaddr;
  530.  
  531.       if (len == sizeof (short))
  532.     return * (unsigned short *) valaddr;
  533.  
  534.       if (len == sizeof (int))
  535.     return * (unsigned int *) valaddr;
  536.  
  537.       if (len == sizeof (long))
  538.     return * (unsigned long *) valaddr;
  539. #ifdef LONG_LONG
  540.       if (len == sizeof (long long))
  541.     return * (unsigned long long *) valaddr;
  542. #endif
  543.     }
  544.   else if (code == TYPE_CODE_INT)
  545.     {
  546.       if (len == sizeof (char))
  547.     return * (char *) valaddr;
  548.  
  549.       if (len == sizeof (short))
  550.     return * (short *) valaddr;
  551.  
  552.       if (len == sizeof (int))
  553.     return * (int *) valaddr;
  554.  
  555.       if (len == sizeof (long))
  556.     return * (long *) valaddr;
  557.  
  558. #ifdef LONG_LONG
  559.       if (len == sizeof (long long))
  560.     return * (long long *) valaddr;
  561. #endif
  562.     }
  563.   else if (code == TYPE_CODE_PTR
  564.        || code == TYPE_CODE_REF)
  565.     {
  566.       if (len == sizeof (char *))
  567.     return (CORE_ADDR) * (char **) valaddr;
  568.     }
  569.   else if (code == TYPE_CODE_MEMBER)
  570.     error ("not implemented: member types in unpack_long");
  571.  
  572.   error ("Value not integer or pointer.");
  573. }
  574.  
  575. /* Return a double value from the specified type and address.
  576.    INVP points to an int which is set to 0 for valid value,
  577.    1 for invalid value (bad float format).  In either case,
  578.    the returned double is OK to use.  */
  579.  
  580. double
  581. unpack_double (type, valaddr, invp)
  582.      struct type *type;
  583.      char *valaddr;
  584.      int *invp;
  585. {
  586.   register enum type_code code = TYPE_CODE (type);
  587.   register int len = TYPE_LENGTH (type);
  588.   register int nosign = TYPE_UNSIGNED (type);
  589. #ifdef KGDB
  590.   union { 
  591.    double    foo; /* double forces alignment. */
  592.    char    buffer[8];
  593.   } tmpMem;
  594.   bcopy(valaddr,tmpMem.buffer,8);
  595.   valaddr = tmpMem.buffer;
  596. #endif
  597.  
  598.   *invp = 0;            /* Assume valid.   */
  599.   if (code == TYPE_CODE_FLT)
  600.     {
  601.       if (INVALID_FLOAT (valaddr, len))
  602.     {
  603.       *invp = 1;
  604.       return 1.234567891011121314;
  605.     }
  606.  
  607.       if (len == sizeof (float))
  608.     return * (float *) valaddr;
  609.  
  610.       if (len == sizeof (double))
  611.     {
  612.       /* Some machines require doubleword alignment for doubles.
  613.          This code works on them, and on other machines.  */
  614.       double temp;
  615.       bcopy ((char *) valaddr, (char *) &temp, sizeof (double));
  616.       return temp;
  617.     }
  618.     }
  619.   else if (code == TYPE_CODE_INT && nosign)
  620.     {
  621.       if (len == sizeof (char))
  622.     return * (unsigned char *) valaddr;
  623.  
  624.       if (len == sizeof (short))
  625.     return * (unsigned short *) valaddr;
  626.  
  627.       if (len == sizeof (int))
  628.     return * (unsigned int *) valaddr;
  629.  
  630.       if (len == sizeof (long))
  631.     return * (unsigned long *) valaddr;
  632.  
  633. #ifdef LONG_LONG
  634.       if (len == sizeof (long long))
  635.     return * (unsigned long long *) valaddr;
  636. #endif
  637.     }
  638.   else if (code == TYPE_CODE_INT)
  639.     {
  640.       if (len == sizeof (char))
  641.     return * (char *) valaddr;
  642.  
  643.       if (len == sizeof (short))
  644.     return * (short *) valaddr;
  645.  
  646.       if (len == sizeof (int))
  647.     return * (int *) valaddr;
  648.  
  649.       if (len == sizeof (long))
  650.     return * (long *) valaddr;
  651.  
  652. #ifdef LONG_LONG
  653.       if (len == sizeof (long long))
  654.     return * (long long *) valaddr;
  655. #endif
  656.     }
  657.  
  658.   error ("Value not floating number.");
  659.   /* NOTREACHED */
  660.   return (double) 0;        /* To silence compiler warning.  */
  661. }
  662.  
  663. /* Given a value ARG1 of a struct or union type,
  664.    extract and return the value of one of its fields.
  665.    FIELDNO says which field.
  666.  
  667.    For C++, must also be able to return values from static fields */
  668.  
  669. value
  670. value_field (arg1, fieldno)
  671.      register value arg1;
  672.      register int fieldno;
  673. {
  674.   register value v;
  675.   register struct type *type = TYPE_FIELD_TYPE (VALUE_TYPE (arg1), fieldno);
  676.   register int offset;
  677.  
  678.   /* Handle packed fields */
  679.  
  680.   offset = TYPE_FIELD_BITPOS (VALUE_TYPE (arg1), fieldno) / 8;
  681.   if (TYPE_FIELD_BITSIZE (VALUE_TYPE (arg1), fieldno))
  682.     {
  683.       v = value_from_long (type,
  684.                unpack_field_as_long (VALUE_TYPE (arg1),
  685.                          VALUE_CONTENTS (arg1),
  686.                          fieldno));
  687.       VALUE_BITPOS (v) = TYPE_FIELD_BITPOS (VALUE_TYPE (arg1), fieldno) % 8;
  688.       VALUE_BITSIZE (v) = TYPE_FIELD_BITSIZE (VALUE_TYPE (arg1), fieldno);
  689.     }
  690.   else
  691.     {
  692.       v = allocate_value (type);
  693.       bcopy (VALUE_CONTENTS (arg1) + offset,
  694.          VALUE_CONTENTS (v),
  695.          TYPE_LENGTH (type));
  696.     }
  697.   VALUE_LVAL (v) = VALUE_LVAL (arg1);
  698.   if (VALUE_LVAL (arg1) == lval_internalvar)
  699.     VALUE_LVAL (v) = lval_internalvar_component;
  700.   VALUE_ADDRESS (v) = VALUE_ADDRESS (arg1);
  701.   VALUE_OFFSET (v) = offset + VALUE_OFFSET (arg1);
  702.   return v;
  703. }
  704.  
  705. value
  706. value_fn_field (arg1, fieldno, subfieldno)
  707.      register value arg1;
  708.      register int fieldno;
  709. {
  710.   register value v;
  711.   struct fn_field *f = TYPE_FN_FIELDLIST1 (VALUE_TYPE (arg1), fieldno);
  712.   register struct type *type = TYPE_FN_FIELD_TYPE (f, subfieldno);
  713.   struct symbol *sym;
  714.  
  715.   sym = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, subfieldno),
  716.                0, VAR_NAMESPACE, 0);
  717.   if (! sym) error ("Internal error: could not find physical method named %s",
  718.             TYPE_FN_FIELD_PHYSNAME (f, subfieldno));
  719.   
  720.   v = allocate_value (type);
  721.   VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
  722.   VALUE_TYPE (v) = type;
  723.   return v;
  724. }
  725.  
  726. /* Return a virtual function as a value.
  727.    ARG1 is the object which provides the virtual function
  728.    table pointer.
  729.    F is the list of member functions which contains the desired virtual
  730.    function.
  731.    J is an index into F which provides the desired virtual function.
  732.    TYPE is the basetype which first provides the virtual function table.  */
  733. value
  734. value_virtual_fn_field (arg1, f, j, type)
  735.      value arg1;
  736.      struct fn_field *f;
  737.      int j;
  738.      struct type *type;
  739. {
  740.   /* First, get the virtual function table pointer.  That comes
  741.      with a strange type, so cast it to type `pointer to long' (which
  742.      should serve just fine as a function type).  Then, index into
  743.      the table, and convert final value to appropriate function type.  */
  744.   value vfn, vtbl;
  745.   value vi = value_from_long (builtin_type_int, 
  746.                   (LONGEST) TYPE_FN_FIELD_VOFFSET (f, j));
  747.   VALUE_TYPE (arg1) = TYPE_VPTR_BASETYPE (type);
  748.  
  749.   /* This type may have been defined before its virtual function table
  750.      was.  If so, fill in the virtual function table entry for the
  751.      type now.  */
  752.   if (TYPE_VPTR_FIELDNO (type) < 0)
  753.     TYPE_VPTR_FIELDNO (type)
  754.       = fill_in_vptr_fieldno (type);
  755.  
  756.   /* The virtual function table is now an array of structures
  757.      which have the form { int16 offset, delta; void *pfn; }.  */
  758.   vtbl = value_ind (value_field (arg1, TYPE_VPTR_FIELDNO (type)));
  759.  
  760.   /* Index into the virtual function table.  This is hard-coded because
  761.      looking up a field is not cheap, and it may be important to save
  762.      time, e.g. if the user has set a conditional breakpoint calling
  763.      a virtual function.  */
  764.   vfn = value_field (value_subscript (vtbl, vi), 2);
  765.  
  766.   /* Reinstantiate the function pointer with the correct type.  */
  767.   VALUE_TYPE (vfn) = lookup_pointer_type (TYPE_FN_FIELD_TYPE (f, j));
  768.   return vfn;
  769. }
  770.  
  771. /* The value of a static class member does not depend
  772.    on its instance, only on its type.  If FIELDNO >= 0,
  773.    then fieldno is a valid field number and is used directly.
  774.    Otherwise, FIELDNAME is the name of the field we are
  775.    searching for.  If it is not a static field name, an
  776.    error is signaled.  TYPE is the type in which we look for the
  777.    static field member.  */
  778. value
  779. value_static_field (type, fieldname, fieldno)
  780.      register struct type *type;
  781.      char *fieldname;
  782.      register int fieldno;
  783. {
  784.   register value v;
  785.   struct symbol *sym;
  786.  
  787.   if (fieldno < 0)
  788.     {
  789.       register struct type *t = type;
  790.       /* Look for static field.  */
  791.       while (t)
  792.     {
  793.       int i;
  794.       for (i = TYPE_NFIELDS (t) - 1; i >= 0; i--)
  795.         if (! strcmp (TYPE_FIELD_NAME (t, i), fieldname))
  796.           {
  797.         if (TYPE_FIELD_STATIC (t, i))
  798.           {
  799.             fieldno = i;
  800.             goto found;
  801.           }
  802.         else
  803.           error ("field `%s' is not static");
  804.           }
  805.       t = TYPE_BASECLASSES (t) ? TYPE_BASECLASS (t, 1) : 0;
  806.     }
  807.  
  808.       t = type;
  809.  
  810.       if (destructor_name_p (fieldname, t))
  811.     error ("use `info method' command to print out value of destructor");
  812.  
  813.       while (t)
  814.     {
  815.       int i, j;
  816.  
  817.       for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; i--)
  818.         {
  819.           if (! strcmp (TYPE_FN_FIELDLIST_NAME (t, i), fieldname))
  820.         {
  821.           error ("use `info method' command to print value of method \"%s\"", fieldname);
  822.         }
  823.         }
  824.       t = TYPE_BASECLASSES (t) ? TYPE_BASECLASS (t, 1) : 0;
  825.     }
  826.       error("there is no field named %s", fieldname);
  827.     }
  828.  
  829.  found:
  830.  
  831.   sym = lookup_symbol (TYPE_FIELD_STATIC_PHYSNAME (type, fieldno),
  832.                0, VAR_NAMESPACE, 0);
  833.   if (! sym) error ("Internal error: could not find physical static variable named %s", TYPE_FIELD_BITSIZE (type, fieldno));
  834.  
  835.   type = TYPE_FIELD_TYPE (type, fieldno);
  836.   v = value_at (type, (CORE_ADDR)SYMBOL_BLOCK_VALUE (sym));
  837.   return v;
  838. }
  839.  
  840. long
  841. unpack_field_as_long (type, valaddr, fieldno)
  842.      struct type *type;
  843.      char *valaddr;
  844.      int fieldno;
  845. {
  846.   long val;
  847.   int bitpos = TYPE_FIELD_BITPOS (type, fieldno);
  848.   int bitsize = TYPE_FIELD_BITSIZE (type, fieldno);
  849.  
  850.   bcopy (valaddr + bitpos / 8, &val, sizeof val);
  851.  
  852.   /* Extracting bits depends on endianness of the machine.  */
  853. #ifdef BITS_BIG_ENDIAN
  854.   val = val >> (sizeof val * 8 - bitpos % 8 - bitsize);
  855. #else
  856.   val = val >> (bitpos % 8);
  857. #endif
  858.  
  859.   val &= (1 << bitsize) - 1;
  860.   return val;
  861. }
  862.  
  863. void
  864. modify_field (addr, fieldval, bitpos, bitsize)
  865.      char *addr;
  866.      int fieldval;
  867.      int bitpos, bitsize;
  868. {
  869.   long oword;
  870.  
  871.   /* Reject values too big to fit in the field in question.
  872.      Otherwise adjoining fields may be corrupted.  */
  873.   if (fieldval & ~((1<<bitsize)-1))
  874.     error ("Value %d does not fit in %d bits.", fieldval, bitsize);
  875.   
  876.   bcopy (addr, &oword, sizeof oword);
  877.  
  878.   /* Shifting for bit field depends on endianness of the machine.  */
  879. #ifdef BITS_BIG_ENDIAN
  880.   bitpos = sizeof (oword) * 8 - bitpos - bitsize;
  881. #endif
  882.  
  883.   oword &= ~(((1 << bitsize) - 1) << bitpos);
  884.   oword |= fieldval << bitpos;
  885.   bcopy (&oword, addr, sizeof oword);
  886. }
  887.  
  888. /* Convert C numbers into newly allocated values */
  889.  
  890. value
  891. value_from_long (type, num)
  892.      struct type *type;
  893.      register LONGEST num;
  894. {
  895.   register value val = allocate_value (type);
  896.   register enum type_code code = TYPE_CODE (type);
  897.   register int len = TYPE_LENGTH (type);
  898.  
  899.   if (code == TYPE_CODE_INT || code == TYPE_CODE_ENUM)
  900.     {
  901.       if (len == sizeof (char))
  902.     * (char *) VALUE_CONTENTS (val) = num;
  903.       else if (len == sizeof (short))
  904.     * (short *) VALUE_CONTENTS (val) = num;
  905.       else if (len == sizeof (int))
  906.     * (int *) VALUE_CONTENTS (val) = num;
  907.       else if (len == sizeof (long))
  908.     * (long *) VALUE_CONTENTS (val) = num;
  909. #ifdef LONG_LONG
  910.       else if (len == sizeof (long long))
  911.     * (long long *) VALUE_CONTENTS (val) = num;
  912. #endif
  913.       else
  914.     error ("Integer type encountered with unexpected data length.");
  915.     }
  916.   else
  917.     error ("Unexpected type encountered for integer constant.");
  918.  
  919.   return val;
  920. }
  921.  
  922. value
  923. value_from_double (type, num)
  924.      struct type *type;
  925.      double num;
  926. {
  927.   register value val = allocate_value (type);
  928.   register enum type_code code = TYPE_CODE (type);
  929.   register int len = TYPE_LENGTH (type);
  930.  
  931.   if (code == TYPE_CODE_FLT)
  932.     {
  933.       if (len == sizeof (float))
  934.     * (float *) VALUE_CONTENTS (val) = num;
  935.       else if (len == sizeof (double))
  936.     * (double *) VALUE_CONTENTS (val) = num;
  937.       else
  938.     error ("Floating type encountered with unexpected data length.");
  939.     }
  940.   else
  941.     error ("Unexpected type encountered for floating constant.");
  942.  
  943.   return val;
  944. }
  945.  
  946. /* Deal with the value that is "about to be returned".  */
  947.  
  948. /* Return the value that a function returning now
  949.    would be returning to its caller, assuming its type is VALTYPE.
  950.    RETBUF is where we look for what ought to be the contents
  951.    of the registers (in raw form).  This is because it is often
  952.    desirable to restore old values to those registers
  953.    after saving the contents of interest, and then call
  954.    this function using the saved values.
  955.    struct_return is non-zero when the function in question is
  956.    using the structure return conventions on the machine in question;
  957.    0 when it is using the value returning conventions (this often
  958.    means returning pointer to where structure is vs. returning value). */
  959.  
  960. value
  961. value_being_returned (valtype, retbuf, struct_return)
  962.      register struct type *valtype;
  963.      char retbuf[REGISTER_BYTES];
  964.      int struct_return;
  965. {
  966.   register value val;
  967.  
  968.   if (struct_return)
  969.     return value_at (valtype, EXTRACT_STRUCT_VALUE_ADDRESS (retbuf));
  970.  
  971.   val = allocate_value (valtype);
  972.   EXTRACT_RETURN_VALUE (valtype, retbuf, VALUE_CONTENTS (val));
  973.  
  974.   return val;
  975. }
  976.  
  977. /* Return true if the function specified is using the structure returning
  978.    convention on this machine to return arguments, or 0 if it is using
  979.    the value returning convention.  FUNCTION is the value representing
  980.    the function, FUNCADDR is the address of the function, and VALUE_TYPE
  981.    is the type returned by the function */
  982.  
  983. struct block *block_for_pc ();
  984.  
  985. int
  986. using_struct_return (function, funcaddr, value_type)
  987.      value function;
  988.      CORE_ADDR funcaddr;
  989.      struct type *value_type;
  990. {
  991.   register enum type_code code = TYPE_CODE (value_type);
  992.  
  993.   if (code == TYPE_CODE_STRUCT ||
  994.       code == TYPE_CODE_UNION ||
  995.       code == TYPE_CODE_ARRAY)
  996.     {
  997.       struct block *b = block_for_pc (funcaddr);
  998.  
  999.       if (!(BLOCK_GCC_COMPILED (b) && TYPE_LENGTH (value_type) < 8))
  1000.     return 1;
  1001.     }
  1002.  
  1003.   return 0;
  1004. }
  1005.  
  1006. /* Store VAL so it will be returned if a function returns now.
  1007.    Does not verify that VAL's type matches what the current
  1008.    function wants to return.  */
  1009.  
  1010. void
  1011. set_return_value (val)
  1012.      value val;
  1013. {
  1014.   register enum type_code code = TYPE_CODE (VALUE_TYPE (val));
  1015.   char regbuf[REGISTER_BYTES];
  1016.   double dbuf;
  1017.   LONGEST lbuf;
  1018.  
  1019.   if (code == TYPE_CODE_STRUCT
  1020.       || code == TYPE_CODE_UNION)
  1021.     error ("Specifying a struct or union return value is not supported.");
  1022.  
  1023.   if (code == TYPE_CODE_FLT)
  1024.     {
  1025.       dbuf = value_as_double (val);
  1026.  
  1027.       STORE_RETURN_VALUE (VALUE_TYPE (val), &dbuf);
  1028.     }
  1029.   else
  1030.     {
  1031.       lbuf = value_as_long (val);
  1032.       STORE_RETURN_VALUE (VALUE_TYPE (val), &lbuf);
  1033.     }
  1034. }
  1035.  
  1036. void
  1037. _initialize_values ()
  1038. {
  1039.   add_info ("convenience", convenience_info,
  1040.         "Debugger convenience (\"$foo\") variables.\n\
  1041. These variables are created when you assign them values;\n\
  1042. thus, \"print $foo=1\" gives \"$foo\" the value 1.  Values may be any type.\n\n\
  1043. A few convenience variables are given values automatically GDB:\n\
  1044. \"$_\"holds the last address examined with \"x\" or \"info lines\",\n\
  1045. \"$__\" holds the contents of the last address examined with \"x\".");
  1046.  
  1047.   add_info ("values", value_history_info,
  1048.         "Elements of value history (around item number IDX, or last ten).");
  1049.   add_info_alias ("history", value_history_info, 0);
  1050. }
  1051.